<template>
	<view>
		<!-- 头像icon -->
		<uni-icon type="person" size="50" class="pericon"></uni-icon>
		<!-- 按钮  事件获取用户信息 -->
		<button type="primary" class="btn" open-type="getUserInfo" @getuserinfo="getinfo">登录</button>
	</view>
</template>

<script>
	export default {
		name: "my-log",
		data() {
			return {

			};
		},
		methods: {
			// 借助button中的open-type 获取用户的信息  uni.getuserinfo()
			getinfo(e) {
				// 判断获取用户信息是否成功
				if (e.detail.errMsg == "getUserInfo:ok") {
					console.log(e.detail.userInfo);
					// 将用户信息存储到user.js中的state
					this.$store.commit("m_user/updateuserinfo", e.detail.userInfo)
					this.getToken(e.detail)
				}

			},
			// 获取token
			async getToken(info) {
				// [null,obj]
				// 获取登录信息
				const [err, res] = await uni.login().catch(err => err)
				// 构成用户信息对象
				const userdata = {
					// uni.login
					code: res.code,
					// uni.getUserInfo
					rawData: info.rawData,
					encryptedData: info.encryptedData,
					signature: info.signature,
					iv: info.iv
				}
				// 更新token
				this.$store.commit("m_user/updatetoken", userdata)
			}
		}
	}
</script>

<style lang="scss">
	/* 布局方式  上中下 ,居中*/
	.pericon {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	.btn {
		width: 90%;
	}

	.logintxt {
		color: #000;
	}
</style>